package com.ca.project.Server.Modules.Tweets.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ca.project.Server.Modules.Tweets.Context.CheckTweetsContext;
import com.ca.project.Server.Modules.Tweets.Context.CreateTweetsContext;
import com.ca.project.Server.Modules.Tweets.Context.UpdateTweetsContext;
import com.ca.project.Server.Modules.Tweets.Converter.TweetsConverter;
import com.ca.project.Server.Modules.Tweets.Entity.Tweets;
import com.ca.project.Server.Modules.Tweets.Service.TweetsService;
import com.ca.project.Server.Modules.Tweets.Mapper.TweetsMapper;
import com.ca.project.Server.Modules.TweetsImg.Service.TweetsImgService;
import com.ca.project.core.Exception.BusinessException;
import com.ca.project.core.Util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author ASUS
 * @description 针对表【tweets】的数据库操作Service实现
 * @createDate 2024-12-31 15:09:27
 */
@Service
public class TweetsServiceImpl extends ServiceImpl<TweetsMapper, Tweets>
        implements TweetsService {

    @Autowired
    private TweetsConverter tweetsConverter;
    @Autowired
    private TweetsService tweetsService;
    @Autowired
    private TweetsImgService tweetsImgService;

    /**
     * 创建推文
     *
     * @param createTweetsContext
     */
    @Override
    public void createTweets(CreateTweetsContext createTweetsContext) {
        findTweetsName(createTweetsContext);
        assembleTweets(createTweetsContext);
        doCreateTweets(createTweetsContext);
    }


    /**
     * 更新推文
     *
     * @param updateTweetsContext
     */
    @Override
    public void updateTweets(UpdateTweetsContext updateTweetsContext) {
        assembleTweetsEntity(updateTweetsContext);
        doUpdate(updateTweetsContext);
    }

    /**
     * 查询推文信息
     *
     * @param checkTweetsContext
     * @return
     */
    @Override
    public Object checkTweets(CheckTweetsContext checkTweetsContext) {
        Long id = checkTweetsContext.getId();
        String t_title = checkTweetsContext.getT_title();
        QueryWrapper<Tweets> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("t_title", t_title);
        return getOne(queryWrapper);
    }

    /**
     * 删除推文
     *
     * @param id
     */
    @Override
    public void deleteById(Long id, String t_title) {
        Tweets entity = findTweets(t_title);
        if (Objects.isNull(entity))
            throw new BusinessException("该推文不存在");
        tweetsImgService.deleteImg(t_title);
        boolean success = baseMapper.deleteById(id) > 0;
        if (!success)
            throw new BusinessException("推文删除失败");
    }


    /**
     * 分页
     *
     * @param currentPage
     * @param pageSize
     * @param tweets
     * @return
     */
    @Override
    public IPage<Tweets> getPage(Integer currentPage, Integer pageSize, Tweets tweets) {
        LambdaQueryWrapper<Tweets> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(tweets.getTTitle()), Tweets::getTTitle, tweets.getTTitle())
                .eq(StringUtils.isNotEmpty(tweets.getTCreateUser()), Tweets::getTCreateUser, tweets.getTCreateUser());
        IPage<Tweets> page = new Page<>(currentPage, pageSize);
        baseMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 分页
     *
     * @param currentPage
     * @param PageSize
     * @return
     */
    @Override
    public IPage<Tweets> getPage(Integer currentPage, Integer PageSize) {
        IPage page = new Page(currentPage, PageSize);
        baseMapper.selectPage(page, null);
        return page;
    }

    /********************************************************private*******************************************************/
    /**
     * 实体转换
     *
     * @param createTweetsContext
     */
    private void assembleTweets(CreateTweetsContext createTweetsContext) {
        Tweets entity = tweetsConverter.CreateTweetsContext2Tweets(createTweetsContext);

        final Long id = IdUtil.get();
        entity.setId(id);
        entity.setTCreateUser(createTweetsContext.getT_create_user());
        entity.setTTitle(createTweetsContext.getT_title());
        entity.setTContent(createTweetsContext.getT_content());
        entity.setTCover(createTweetsContext.getT_cover());
        entity.setRadio(Integer.valueOf(createTweetsContext.getRadio()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());

        createTweetsContext.setEntity(entity);
    }

    /**
     * 数据库
     *
     * @param createTweetsContext
     */
    private void doCreateTweets(CreateTweetsContext createTweetsContext) {
        Tweets entity = createTweetsContext.getEntity();

        if (entity == null)
            throw new BusinessException("推文实体不能为空");

        try {
            if (!save(entity))
                throw new BusinessException("推文创建失败");
        } catch (Exception e) {
            throw new BusinessException("推文创建失败,原因:" + e.getMessage());
        }

    }

    /**
     * 实体转化
     *
     * @param updateTweetsContext
     */
    private void assembleTweetsEntity(UpdateTweetsContext updateTweetsContext) {
        Tweets entity = tweetsConverter.UpdateTweetsContext2Tweets(updateTweetsContext);

        entity.setTCreateUser(updateTweetsContext.getT_create_user());
        entity.setTTitle(updateTweetsContext.getT_title());
        entity.setTContent(updateTweetsContext.getT_content());
        entity.setUpdateTime(new Date());

        updateTweetsContext.setEntity(entity);
    }

    /**
     * 更新
     *
     * @param updateTweetsContext
     */
    private void doUpdate(UpdateTweetsContext updateTweetsContext) {
        Tweets entity = updateTweetsContext.getEntity();

        if (Objects.isNull(entity))
            throw new BusinessException("推文实体不能为空");

        UpdateWrapper<Tweets> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", updateTweetsContext.getId());

        boolean success = tweetsService.update(entity, wrapper);
        if (!success)
            throw new BusinessException("用户信息更新失败");
    }

    /**
     * 找该推文名称是否被占用
     *
     * @param createTweetsContext
     * @return
     */
    private void findTweetsName(CreateTweetsContext createTweetsContext) {
        String tTitle = createTweetsContext.getT_title();
        Tweets entity = findTweets(tTitle);
        if (Objects.nonNull(entity))
            throw new BusinessException("推文已经创建");
    }

    /**
     * 找推文是否存在
     *
     * @param t_title
     * @return
     */
    private Tweets findTweets(String t_title) {
        QueryWrapper<Tweets> wrapper = new QueryWrapper<>();
        wrapper.eq("t_title", t_title);
        return getOne(wrapper);
    }
}




